const app = getApp()

import {
  showMsg
} from '../../../../utils/config.js'
import {
  reqAlarmList,
  reqProductList,
} from '../../../../request/api'
let dictMixin = require('../../../behaviors/dict/dictMixin.js')

Component({
  behaviors: [dictMixin],
  options: {
    addGlobalClass: true
  },

  properties: {

  },

  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
    indexBg: '/statics/images/index_bg.png',
    isLogin: false,
    tenantId: '',

    workList: [{
        id: 0,
        url: '/statics/images/device.png',
        text: '设备',
      }, {
        id: 1,
        url: '/statics/images/building.png',
        text: '建筑物',
      },
      {
        id: 2,
        url: '/statics/images/people_call.png',
        text: '接警人',
      },
      // {
      //     id: 4,
      //     url: '/statics/images/team.png',
      //     text: '团队管理',
      //   
      // },
    ],

    productList: [], //产品列表
    dataList: [],
    page: 1,
    isNextPage: true,
    triggered: false, //是否下拉刷新告警列表
    isProprietor: true, //装维||业主
  },
  lifetimes: {
      // 初始化数据或绑定方法
    created() {
      this.getDict('device_alarm_type');
    },
    // 组件挂载到页面时触发
    attached() {
      this.setData({
        isLogin: app.globalData.token ? true : false,
        tenantId: app.globalData.tenantId,
        isProprietor: app.globalData.isProprietor
      })
      console.log(this.data.isLogin);
      if (!this.data.isLogin) return;
      this.setNavigationBarColorFun()
      this.getProductList()
      this.setWork()
    },

  },

  pageLifetimes: {
    show() {
      console.log('adminshow');
      this.getTabBar().init();
      this.getInitDataList()
    },
  },

  methods: {
    setNavigationBarColorFun() {
      wx.setNavigationBarColor({
        backgroundColor: '#ffffff',
        frontColor: '#ffffff',
      })
    },
    // 设置工作台
    setWork() {
      let {
        workList
      } = this.data
      if (this.data.tenantId && this.data.tenantId != '0' && this.data.tenantId.length > 5) {
        // 个人注册用户不拥有隐患功能
        workList.push({
          id: 7,
          url: '/statics/images/danger.png',
          text: '隐患上报',
        })
        this.setData({
          workList
        })
      }

      if (app.globalData.isProprietor) {
        // 业主进来
        workList = [{
          id: 0,
          url: '/statics/images/device.png',
          text: '设备',
        }, {
          id: 7,
          url: '/statics/images/danger.png',
          text: '隐患上报',
        }]
      }
      this.setData({
        workList: workList
      })

    },


    // 产品列表
    async getProductList() {
      const res = await reqProductList()
      if (res.code !== 20000) return showMsg(res.message)
      let {
        rows
      } = res.data
      this.setData({
        productList: rows
      })
      this.getInitDataList()
    },
    /**
     * @name: 获取告警列表
     * @msg:
     * @return {*}
     */
    async getDataList() {
      let params = {
        tenantId: app.globalData.tenantId,
        userId: app.globalData.userId,
        roleName: app.globalData.rolename,
        orgId: '',
        status: 1
      }
      const res = await reqAlarmList(this.data.page, 10, params)
      if (res.code !== 20000) return showMsg(res.message)
      let {
        rows
      } = res.data

      let {
        productList,
        dictData
      } = this.data
      if (rows && rows.length) {
        rows.forEach(item => {
          productList.forEach(citem => {
            if (item.deviceType === citem.deviceType) {
              item.type = citem.name //产品类型
            }
          })
          dictData.forEach(dict => {
            if (item.alarmType == dict.dickValue) {
              item.dickLabel = dict.dickLabel; //告警类型
            }
          })
        });
      }

      if (this.data.page === 1) {
        this.setData({
          dataList: rows,
          isNextPage: true
        })
      } else if (this.data.page > 1 && (rows && rows.length)) {
        this.setData({
          dataList: this.data.dataList.concat(rows),
          isNextPage: true
        })
      } else if (this.data.page > 1 && (!rows || !rows.length)) {
        showMsg('已经到底啦~')
        this.setData({
          isNextPage: false
        })
      }
    },
    // 滚动到底部触发
    lower() {
      if (!this.data.isNextPage) return
      this.setData({
        page: this.data.page + 1,
      })
      this.getDataList()
    },
    // 下拉刷新
    async onRefresh() {
      this.setData({
        dataList: [],
        page: 1,
        isNextPage: true,
        triggered: true,
      })
      await this.getDataList()
      this.setData({
        triggered: false,
      })
    },
    // 操作列表后回到最初状态
    getInitDataList() {
      this.setData({
        page: 1,
        isNextPage: true,
      })
      this.getDataList()
    },
    // 去告警详情
    goAlarmDetail(e) {
      let {
        item
      } = e.currentTarget.dataset
      let info = JSON.stringify(item)
      wx.navigateTo({
        url: `/pages/message-index-page/alarm-handle/alarm-handle?info=${info}`,
      })
    },
    // 点击复制imei
    clickCopyImei(e) {
      let {
        imei
      } = e.currentTarget.dataset
      console.log(e);
      console.log(imei);
      wx.setClipboardData({
        data: imei,
        success(res) {
          wx.getClipboardData({
            success(res) {
              showMsg(`${imei} 已复制`)
            }
          })
        }
      })
    },
    /**
     * @name: 去功能页
     * @msg: 根据  text字段配对
     * @return {*}
     */
    goWorkPage(e) {
      let {
        text
      } = e.currentTarget.dataset
      if (text === '设备') {
        if (app.globalData.isProprietor) {
          // 业主跳转
          wx.navigateTo({
            url: '/packageA/pages/index-inside-page/owner-device/owner-device',
          })
        } else {
          wx.navigateTo({
            url: '/packageA/pages/index-inside-page/device/device',
          })
        }
      } else if (text === '建筑物') {
        wx.navigateTo({
          url: '/packageA/pages/index-inside-page/building/building',
        })
      } else if (text === '接警人') {
        wx.navigateTo({
          url: '/packageA/pages/index-inside-page/alarm-receiver/alarm-receiver',
        })
      } else if (text === '团队管理') {
        if (app.globalData.orgId == 1) return showMsg("请先选择单位！")
        wx.navigateTo({
          url: '/packageA/pages/personal-inside-page/team/team',
        })
      } else if (text === '隐患上报') {
        wx.navigateTo({
          url: '/packageA/pages/index-inside-page/hiddenDanger/hiddenDanger',
        })
      }
    },
  }
})